package com.game.character;

import android.graphics.Point;
import android.util.Log;

import com.game.bullet.BulletManager;
import com.game.collision.CollisionCheck;
import com.game.framwork.gamesupport.GameSprite;
import com.game.framwork.itf.Graphics;
import com.game.pathFinding.PathFinding;
import com.game_ltdd.Resrouce;

public class RageMonster extends Mobs {
	private PathFinding pathFind;
	private float timeStartFindPath;

	public RageMonster(int typeMonster) {
		timeStartFindPath = 0;
		mobType = typeMonster;
		timeMaxDelayShot = 1.5f;
		this.runSpeed = 4.0f;
		maxDieTime = 1.0f;
		init();
	}

	public void init() {
		GameSprite[] pAnim;
		pAnim = new GameSprite[7];

		if (mobType == MOB_MUMMYRAGE) {
			this.objectImage = Resrouce.ragemummy;
			pAnim[0] = new GameSprite(objectImage);
			pAnim[0].createAnimation("Stand", 8, 8, 8, 1.0f);
			pAnim[1] = new GameSprite(objectImage);
			pAnim[1].createAnimation("Stand Animation", 0, 8, 0, 1.0f / 10.0f);
			pAnim[2] = new GameSprite(objectImage);
			pAnim[2].createAnimation("Move Left", 9, 14, 9, 1.0f / 10.0f);
			pAnim[3] = new GameSprite(objectImage);
			pAnim[3].createAnimation("Move Right", 15, 20, 15, 1.0f / 10.0f);
			pAnim[4] = new GameSprite(objectImage);
			pAnim[4].createAnimation("Move Top", 21, 26, 21, 1.0f / 10.0f);
			pAnim[5] = new GameSprite(objectImage);
			pAnim[5].createAnimation("Move Bot", 27, 31, 27, 1.0f / 6.0f);
			pAnim[6] = new GameSprite(objectImage);
			pAnim[6].createAnimation("Die", 27, 27, 27, 1.0f / 6.0f);

			loadSprite(objectImage, 32, 10, 56, 76, pAnim);
			createBoundBox(12, 15, 45, 30/* 20.0f, 20.0f, 30, 25 */);
		} else if (mobType == MOB_BOSSRAGE) {
			this.objectImage = Resrouce.rageboss;
			pAnim[0] = new GameSprite(objectImage);
			pAnim[0].createAnimation("Stand", 15, 15, 15, 1.0f);
			pAnim[1] = new GameSprite(objectImage);
			pAnim[1].createAnimation("Stand Animation", 15, 15, 15, 1.0f / 4.0f);
			pAnim[2] = new GameSprite(objectImage);
			pAnim[2].createAnimation("Move Left", 0, 4, 0, 1.0f / 6.0f);
			pAnim[3] = new GameSprite(objectImage);
			pAnim[3].createAnimation("Move Right", 5, 9, 5, 1.0f / 6.0f);
			pAnim[4] = new GameSprite(objectImage);
			pAnim[4].createAnimation("Move Top", 10, 14, 10, 1.0f / 6.0f);
			pAnim[5] = new GameSprite(objectImage);
			pAnim[5].createAnimation("Move Bot", 15, 19, 15, 1.0f / 6.0f);
			pAnim[6] = new GameSprite(objectImage);
			pAnim[6].createAnimation("Die", 15, 15, 15, 1.0f / 6.0f);

			loadSprite(objectImage, 32, 10, 56, 67, pAnim);
			createBoundBox(9, 20, 35, 25);
		}
	}

	private void shot(float deltaTime) {
		timeDelayShot += deltaTime;
		if (timeDelayShot >= timeMaxDelayShot) {
			BulletManager.getInstance().createBullet(this);
			timeDelayShot = 0f;
		}
	}

	@Override
	public void updateInternal(float deltaTime) {
		if (!isDeath && !isDying) {
			shot(deltaTime);
			timeToChangeDir += deltaTime;
			timeStartFindPath += deltaTime;
			if (currState == 0) {
				this.changeAnimation("Stand");
			}
			pathFind = new PathFinding(tiledMap);
			mapPos = tiledMap.convertToMapIndex(position);
			if (timeStartFindPath < 0 /*>= 1f*/) {	
				if (pathFind.Dijkstra_Alg(mapPos, new Point(356 / 20, 356 % 20)) == 0) {
					Log.i("debug", " " + pathFind.posNext.x + pathFind.posNext.y);
					if (mapPos.x < pathFind.posNext.x) {
						currState = MOVE_RIGHT;
					}
					if (mapPos.x > pathFind.posNext.x) {
						currState = MOVE_LEFT;
					}
					if (mapPos.y < pathFind.posNext.y) {
						currState = MOVE_BOTTOM;
					}
					if (mapPos.y > pathFind.posNext.y) {
						currState = MOVE_TOP;
					}
				} else {
					//randomMove();
				}
				timeStartFindPath = 0;
			} else {
				randomMove();
			}
			CollisionCheck.collisionOnMap(this, tiledMap);
			CollisionCheck.collisionWithBullet(this,
					BulletManager.getInstance().listBulletOfPlayer);
		} else if (isDying) {
			dying(deltaTime);
		}
		super.updateInternal(deltaTime);
	}

	private void dying(float deltaTime) {
		changeAnimation("Die");
		dieTime += deltaTime;
		if (dieTime >= maxDieTime) {
			dieTime = 0.0f;
			isDeath = true;
		}
	}

	@Override
	public void paintInternal(Graphics g) {
		if (!isDeath) {
			super.paintInternal(g);
		}
	}
}
